package com.ct.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDate();
        //测试代码。。。
        initText();
        //测试ctas
        initCtas();

    }



    private void initCtas() {
    }
    private void initDate() {
    }

    private void initText() {

    }


    private void initView() {
        mWebView= (WebView) findViewById(R.id.webView);
        mProgressBar= (ProgressBar) findViewById(R.id.progress);
        //支持js
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        //加载urlhttps://auth.alipay.com/login/index.htm
        mWebView.loadUrl("https://auth.alipay.com/login/index.htm");


        //将js转化为string


        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

               // mWebView.loadDataWithBaseURL("http://192.168.39.214/kdkj/h5/web/data_capture/", "data_capture.js", "");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final String result = jsToString();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mWebView.loadUrl("javascript:" + result);
                            }
                        });
                    }
                }).start();
//
//                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
//                Log.d("CTAS00", result);

            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    // 网页加载完成
                    mProgressBar.setVisibility(View.GONE);

                } else {
                    // 加载中
                    mProgressBar.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    private String jsToString() {

        try {
            URL url = new URL("http://192.168.39.214/kdkj/h5/web/data_capture/data_capture.js");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            String wholeJS = null;
            InputStream in = urlConnection.getInputStream();
            byte buff[] = new byte[1024];
            ByteArrayOutputStream fromFile = new ByteArrayOutputStream();
            FileOutputStream out = null;
            do {
                int numread = in.read(buff);
                if (numread <= 0) {
                    break;
                }
                fromFile.write(buff, 0, numread);
            } while (true);
             wholeJS = fromFile.toString();
            return wholeJS;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(mWebView.canGoBack())
            {
                mWebView.goBack();//返回上一页面
                return true;
            }
            else
            {
                System.exit(0);//退出程序
            }
        }


        return super.onKeyDown(keyCode, event);
    }
}
